/*
 * 版权所有 (C) 2001-2014 深圳市艾派应用系统有限公司。保留所有权利。
 * 版本：
 * 修改记录：
 *		1、2014-12-5，tfl创建。 
 */
package com.chinamobile.ln.ictenterprises.widget;

import com.chinamobile.ln.ictenterprises.R;
import com.chinamobile.ln.ictenterprises.utils.StringUtil;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.EditText;
import android.widget.TextView;

public class InputContactEditText extends EditText
{
    private Paint mPaint;
    private Context context;

    public InputContactEditText(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.BLACK);
        this.context = context;
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        String phone = getText().toString();
        int length = getText().length();
        if (length > 1)
        {
            if (getText().charAt(length - 1) == ',')
            {
                String phoneNum = phone.substring(0, phone.length() - 1);
                addEditTextName(phoneNum);
            }
        }

    }

    /**
     * 显示添加联系人
     * @param mTextName
     */
    private void addEditTextName(String mTextName)
    {
        setText(null);
        TextView mTextView = new TextView(context);
        mTextView.setText(mTextName);
        mTextView.setPadding(6, 4, 6, 4);
        mTextView.setTextSize(14);
        mTextView.setGravity(Gravity.CENTER);
        // mTextView.setBackgroundColor(getResources().getColor(R.color.blue));
        mTextView.setTextColor(getResources().getColor(R.color.blue));
        mTextView.setDrawingCacheEnabled(true);
        mTextView.measure(
            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        mTextView.layout(0, 0, mTextView.getMeasuredWidth(),
            mTextView.getMeasuredHeight());
        Bitmap newBitmap = mTextView.getDrawingCache();// TextView转换为Bitmap
        // 使用ImageSpan
        ImageSpan imageSpan = new ImageSpan(context, newBitmap);
        SpannableString spannableString = new SpannableString(mTextName);
        spannableString.setSpan(imageSpan, 0, mTextName.length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);// 把固定字符串转换为imagespan
        getText().append(spannableString);// 添加到EditText中
        getText().append("、");
    }
}
